package com.iraeta.controller.report;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.iraeta.common.Result;
import com.iraeta.common.ServiceException;
import com.iraeta.controller.BaseController;
import com.iraeta.entity.main.Page;
import com.iraeta.entity.main.Role;
import com.iraeta.entity.main.User;
import com.iraeta.entity.main.resulttype.PageUserVo;
import com.iraeta.service.basic.PageService;
import com.iraeta.service.sys.RoleService;
import com.iraeta.service.sys.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController(value = "PageAdminController")
@RequestMapping("pageadmin")
public class PageAdminController extends BaseController {
    @Autowired
    PageService pageService;
    @Autowired
    RoleService roleService;
    @Autowired
    UserService userService;

    @RequestMapping("listpage")
    public String listPage(@RequestBody JSONObject jo){
        String pageName = jo.getString("pageName");
        List<Page> pageList = pageService.listPage(pageName);
        return this.success(pageList);
    }

    /*查看一个page界面有哪些角色可以看*/
    @RequestMapping("listpagerole")
    public String listPageRole(@RequestBody JSONObject jo){
        Integer pageId = jo.getInteger("pageId");
        List<Role> roleList =  roleService.getRoleByPermissionId(pageId);
        return this.success(roleList);
    }

    /*查看一个page界面有哪些角色可以看*/
    @RequestMapping("listpageuser")
    public String listPageUser(@RequestBody JSONObject jo){
        Integer pageId = jo.getInteger("pageId");
        List<User> userList = userService.listUserByName("");
        List<User> pageUserList = pageService.listPageUser(pageId);
        JSONObject res = new JSONObject();
        res.put("allUser", userList);
        res.put("pageUser", pageUserList);
        return this.success(res);
    }

    @RequestMapping("savepageuser")
    public String savePageUser(@RequestBody JSONObject paramJb){
        int pageId = paramJb.getIntValue("pageId");
        JSONArray pageUsersJa = paramJb.getJSONArray("pageUsers");
        List<Integer> pageUsers = new ArrayList<>();
        for(int i =0; i<pageUsersJa.size(); i++){
            pageUsers.add(pageUsersJa.getIntValue(i));
        }
        try {
            pageService.savePageUser(pageId, pageUsers);
            return JSON.toJSONString(Result.success());
        }catch (Exception e){
            throw new ServiceException(e.getMessage());
        }
    }

    /*查看一个用户拥有的page界面*/
    @RequestMapping("listuserpage")
    public String listUserPage(@RequestBody JSONObject jo){
        int usedId = this.getUserId();
        List<Page> userPageList = pageService.listUserPage(usedId);
        return this.success(userPageList);
    }
}
